<?php

namespace app\admin\controller;

use app\admin\model\Chapter;
use app\admin\model\Course;
use think\Controller;
use think\Request;
use think\Url;

class Chapters extends Controller
{

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create($course_id)
    {
        //
        $id = $course_id;

        return view('create', compact('id'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request, $course_id)
    {
        Chapter::create($request->param());
//        $chapter->course_id = $course_id;
//        $chapter->save();
        return redirect(Url('Chapters/read', ['id' => $course_id]));
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //

        $chapters = Chapter::where('course_id', $id)->select();
//        return json($chapters);
        return view('read', compact('chapters', 'id'));
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $chapter = Chapter::find($id);
//        return json($chapter);
        //
        return view('edit', compact('chapter'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $chapter = Chapter::find($id);
        $chapter->update($request->param());
        $this->success('修改章节成功啦！', Url('Chapters/read', ['id' => $chapter['course_id']]));

    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        $chapter = Chapter::find($id);
        Chapter::destroy($id);
        $this->success('删除成功', Url('Chapters/read', ['id' => $chapter['course_id']]));
    }
}
